﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrmContacto.Contracts.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.Contracts.Enums;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.CrmContacto.Code;
using Web.Framework;
using Web.Framework.Translate;

namespace CrmContacto.WebApp.App
{
    public partial class PlanDeMarketingList : BasePage
    {
        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton nuevaCategoriaButton = new LinkButton();
            nuevaCategoriaButton.ID = "BoPlanDeMarketingActionMenuNuevaPlanDeMarketing";
            nuevaCategoriaButton.Text = LocalizationProvider.Instance["Bo.PlanDeMarketing.ActionMenu.NuevaPlanDeMarketing"];
            nuevaCategoriaButton.Click += new EventHandler(nuevaCampaniaButton_Click);

            actionButtons.Add(nuevaCategoriaButton);
            base.LoadContextActions(actionButtons);
        }

        private void nuevaCampaniaButton_Click(object sender, EventArgs e)
        {
            AgregarNuevoPlanDeMarketing();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.lnkBuscar.Attributes.Add("aspnetid", this.lnkBuscar.ClientID);
                // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
                if (!this.IsPostBack)
                {
                    LoadCbo();
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void LoadCbo()
        {
            UIHelper.LoadEstados<EstadoPlanDeMarketing>(this.cboEstado, LocalizationProvider.Instance["DropDown.Todos"]);
        }

        protected override void ApplySecurity()
        {
            //this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            //if (this.GridPlanDeMarketing.DataSource != null)
            //{
            //    this.GridPlanDeMarketing.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            //    this.GridPlanDeMarketing.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            //}
        }

        private void Buscar()
        {
            PlanDeMarketingSearch search = new PlanDeMarketingSearch();
            search.NombreLike = this.txtNombre.Text;
            search.Estado = UIHelper.GetIntFromInputCbo(this.cboEstado);
            search.EmpresaId = BasePage.CompanyId;

            DateTime fechaDesde;
            if (DateTime.TryParseExact(this.txtVigenciaDesde.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out fechaDesde))
            {
                search.VigenciaDesde = fechaDesde;
            }

            DateTime fechaHasta;
            if (DateTime.TryParseExact(this.txtVigenciaHasta.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out fechaHasta))
            {
                search.VigenciaHasta = fechaHasta;
            }

            List<PlanDeMarketing> planDeMarketingList = WebServiceHelper.GetPlanesDeMarketing(search);

            this.GridPlanDeMarketing.DataSource = planDeMarketingList;
            this.GridPlanDeMarketing.DataBind();

            pnlResults.Visible = true;
        }

        protected void GridPlanDeMarketing_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    PlanDeMarketing planDeMarketing = (PlanDeMarketing)e.Row.DataItem;

                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    SetJavascriptForDeleteRecord(btnEliminarCtrl, planDeMarketing.Nombre);                    

                    // Estado
                    if (planDeMarketing.Estado.HasValue)
                    {
                        e.Row.Cells[3].Text = UIHelper.GetEnumDescription((EstadoPlanDeMarketing)planDeMarketing.Estado.Value);
                    }
                    else
                    {
                        e.Row.Cells[3].Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridPlanDeMarketing_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int planDeMarketingId = Convert.ToInt32(this.GridPlanDeMarketing.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPlanDeMarketingEdit + "?Id={0}", planDeMarketingId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int planDeMarketingId = Convert.ToInt32(this.GridPlanDeMarketing.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarPlanDeMarketing(planDeMarketingId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridPlanDeMarketing_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridPlanDeMarketing.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            AgregarNuevoPlanDeMarketing();
        }

        private void AgregarNuevoPlanDeMarketing()
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPlanDeMarketingEdit, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}